import model.*;
import service.PropertyService;

import java.util.Date;
import java.util.List;
import java.util.Scanner;
//居民小区物业管理系统
//实现房屋与业主信息管理、能够根据房屋面积计算房屋物业费、记录物业费收缴情况。
//实现小区停车位管理，停车位分为售出停车位（所有人管理）和租用停车位（租用人管理），售出停车位按年计算管理费，租用停车位按月计算租用费。

public class Main {
    public static void main(String[] args) throws Exception {
        PropertyService service = new PropertyService();
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("==== 小区物业管理系统 ====");
            System.out.println("1. 业主信息管理（增删改查）");
            System.out.println("2. 房屋信息管理（增删改查）");
            System.out.println("3. 房屋权限管理（添加/删除业主对房屋的所有权）");
            System.out.println("4. 计算并记录房屋物业费");
            System.out.println("5. 查看物业费收缴情况");
            System.out.println("6. 添加停车位");
            System.out.println("7. 查看停车位");
            System.out.println("0. 退出");
            System.out.print("请选择：");
            int op = sc.nextInt();
            sc.nextLine();

            if (op == 1) {
                System.out.println("1. 添加业主");
                System.out.println("2. 修改业主");
                System.out.println("3. 删除业主");
                System.out.println("4. 查询单个业主");
                System.out.println("5. 查询所有业主");
                System.out.print("请选择业主操作：");
                int subOp = sc.nextInt();
                sc.nextLine();
                if (subOp == 1) {
                    System.out.print("业主ID: ");
                    int id = sc.nextInt();
                    sc.nextLine();
                    System.out.print("姓名: ");
                    String name = sc.nextLine();
                    System.out.print("电话: ");
                    String phone = sc.nextLine();
                    try {
                        service.addOwner(new Owner(id, name, phone));
                        System.out.println("添加成功");
                    } catch (Exception e) {
                        System.out.println("添加失败：" + e.getMessage());
                    }
                } else if (subOp == 2) {
                    System.out.print("业主ID: ");
                    int id = sc.nextInt();
                    sc.nextLine();
                    // 判断业主是否存在
                    Owner existOwner = service.getOwnerById(id);
                    if (existOwner == null) {
                        System.out.println("业主不存在");
                        return; 
                    }
                    System.out.print("新姓名: ");
                    String name = sc.nextLine();
                    System.out.print("新电话: ");
                    String phone = sc.nextLine();
                    try {
                        service.updateOwner(new Owner(id, name, phone));
                        System.out.println("修改成功");
                    } catch (Exception e) {
                        System.out.println("修改失败：" + e.getMessage());
                    }
                } else if (subOp == 3) {
                    System.out.print("业主ID: ");
                    int id = sc.nextInt();
                    try {
                        service.deleteOwner(id);
                        System.out.println("删除成功");
                    } catch (Exception e) {
                        System.out.println("删除失败：" + e.getMessage());
                    }
                } else if (subOp == 4) {
                    System.out.print("业主ID: ");
                    int id = sc.nextInt();
                    try {
                        Owner o = service.getOwnerById(id);
                        if (o != null) {
                            System.out.println("ID:" + o.getId() + " 姓名:" + o.getName() + " 电话:" + o.getPhone());
                            // 查询拥有的房屋
                            List<String> houseIds = service.getHouseIdsByOwnerId(id);
                            if (houseIds.isEmpty()) {
                                System.out.println("拥有房屋: 无");
                            } else {
                                System.out.print("拥有房屋ID: ");
                                for (String hid : houseIds) {
                                    System.out.print(hid + " ");
                                }
                                System.out.println();
                            }
                        } else
                            System.out.println("未找到该业主");
                    } catch (Exception e) {
                        System.out.println("查询失败：" + e.getMessage());
                    }
                } else if (subOp == 5) {
                    try {
                        List<Owner> owners = service.getAllOwners();
                        for (Owner o : owners) {
                            System.out.print("ID:" + o.getId() + " 姓名:" + o.getName() + " 电话:" + o.getPhone());
                            // 查询拥有的房屋
                            List<String> houseIds = service.getHouseIdsByOwnerId(o.getId());
                            if (houseIds.isEmpty()) {
                                System.out.println(" 拥有房屋: 无");
                            } else {
                                System.out.print(" 拥有房屋ID: ");
                                for (String hid : houseIds) {
                                    System.out.print(hid + " ");
                                }
                                System.out.println();
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("查询失败：" + e.getMessage());
                    }
                }
            } else if (op == 2) {
                System.out.println("1. 添加房屋");
                System.out.println("2. 修改房屋");
                System.out.println("3. 删除房屋");
                System.out.println("4. 查询单个房屋");
                System.out.println("5. 查询所有房屋");
                System.out.print("请选择房屋操作：");
                int subOp = sc.nextInt();
                sc.nextLine();
                if (subOp == 1) {
                    System.out.print("房屋ID: ");
                    String id = sc.nextLine();
                    System.out.print("面积: ");
                    double area = sc.nextDouble();
                    try {
                        service.addHouse(new House(id, area));
                        System.out.println("添加成功");
                    } catch (Exception e) {
                        System.out.println("添加失败：" + e.getMessage());
                    }
                } else if (subOp == 2) {
                    System.out.print("房屋ID: ");
                    String id = sc.nextLine();
                    // 判断房屋是否存在
                    House existHouse = service.getHouseById(id);
                    if (existHouse == null) {
                        System.out.println("房屋不存在");
                        continue; // 返回主菜单
                    }
                    System.out.print("新面积: ");
                    double area = sc.nextDouble();
                    try {
                        service.updateHouse(new House(id, area));
                        System.out.println("修改成功");
                    } catch (Exception e) {
                        System.out.println("修改失败：" + e.getMessage());
                    }
                } else if (subOp == 3) {
                    System.out.print("房屋ID: ");
                    String id = sc.nextLine();
                    try {
                        service.deleteHouse(id);
                        System.out.println("删除成功");
                    } catch (Exception e) {
                        System.out.println("删除失败：" + e.getMessage());
                    }
                } else if (subOp == 4) {
                    System.out.print("房屋ID: ");
                    String id = sc.nextLine();
                    try {
                        House h = service.getHouseById(id);
                        if (h != null) {
                            List<String> ownerNames = service.getOwnerNamesByHouseId(id);
                            System.out.print("ID:" + h.getId() + " 面积:" + h.getArea());
                            if (ownerNames.isEmpty()) {
                                System.out.println(" 拥有者: 无");
                            } else {
                                System.out.print(" 拥有者: ");
                                for (String name : ownerNames) {
                                    System.out.print(name + " ");
                                }
                                System.out.println();
                            }
                        } else
                            System.out.println("未找到该房屋");
                    } catch (Exception e) {
                        System.out.println("查询失败：" + e.getMessage());
                    }
                } else if (subOp == 5) {
                    try {
                        List<House> houses = service.getAllHouses();
                        for (House h : houses) {
                            List<String> ownerNames = service.getOwnerNamesByHouseId(h.getId());
                            System.out.print("ID:" + h.getId() + " 面积:" + h.getArea());
                            if (ownerNames.isEmpty()) {
                                System.out.println(" 拥有者: 无");
                            } else {
                                System.out.print(" 拥有者: ");
                                for (String name : ownerNames) {
                                    System.out.print(name + " ");
                                }
                                System.out.println();
                            }
                        }
                    } catch (Exception e) {
                        System.out.println("查询失败：" + e.getMessage());
                    }
                }
            } else if (op == 3) {
                // 房屋权限管理
                System.out.println("1. 添加业主对房屋的所有权");
                System.out.println("2. 删除业主对房屋的所有权");
                System.out.print("请选择操作：");
                int subOp = sc.nextInt();
                sc.nextLine();
                if (subOp == 1) {
                    System.out.print("业主ID: ");
                    int ownerId = sc.nextInt();
                    sc.nextLine();
                    System.out.print("房屋ID: ");
                    String houseId = sc.nextLine();
                    try {
                        service.addOwnerToHouse(ownerId, houseId);
                    } catch (Exception e) {
                        System.out.println("添加失败：" + e.getMessage());
                    }
                } else if (subOp == 2) {
                    System.out.print("业主ID: ");
                    int ownerId = sc.nextInt();
                    sc.nextLine();
                    System.out.print("房屋ID: ");
                    String houseId = sc.nextLine();
                    try {
                        service.removeOwnerFromHouse(ownerId, houseId);
                    } catch (Exception e) {
                        System.out.println("删除失败：" + e.getMessage());
                    }
                }
            } else if (op == 4) {
                System.out.print("物业费ID: ");
                int id = sc.nextInt();
                sc.nextLine();
                System.out.print("房屋ID: ");
                String houseId = sc.nextLine();
                System.out.print("面积: ");
                double area = sc.nextDouble();
                double amount = service.calculatePropertyFee(area);
                System.out.print("是否已缴费(1是0否): ");
                boolean paid = sc.nextInt() == 1;
                System.out.print("到期时间(时间戳): ");
                long due = sc.nextLong();
                service.addPropertyFee(new PropertyFee(id, houseId, amount, paid, new java.util.Date(due)));
                System.out.println("记录成功");
            } else if (op == 5) {
                List<PropertyFee> fees = service.getAllPropertyFees();
                for (PropertyFee f : fees) {
                    System.out.println("ID:" + f.getId() + " 房屋ID:" + f.getHouseId() + " 金额:" + f.getAmount() + " 已缴:" + f.isPaid() + " 到期:" + f.getDueDate());
                }
            } else if (op == 6) {
                System.out.print("停车位ID: ");
                int id = sc.nextInt();
                sc.nextLine();
                System.out.print("类型(sold/rented): ");
                String type = sc.nextLine();
                Integer ownerId = null, renterId = null;
                double fee = 0;
                if ("sold".equals(type)) {
                    System.out.print("所有人ID: ");
                    ownerId = sc.nextInt();
                    fee = service.calculateSoldParkingFee();
                } else if ("rented".equals(type)) {
                    System.out.print("租用人ID: ");
                    renterId = sc.nextInt();
                    fee = service.calculateRentedParkingFee();
                }
                service.addParkingSpace(new ParkingSpace(id, type, ownerId, renterId, fee));
                System.out.println("添加成功");
            } else if (op == 7) {
                List<ParkingSpace> psList = service.getAllParkingSpaces();
                for (ParkingSpace ps : psList) {
                    System.out.println("ID:" + ps.getId() + " 类型:" + ps.getType() + " 所有人:" + ps.getOwnerId() + " 租用人:" + ps.getRenterId() + " 费用:" + ps.getFee());
                }
            } else if (op == 0) {
                break;
            }
        }
        sc.close();
    }
}
